package com.lkm.app_ui.m.Imple.hsj522;

import com.lkm.frame.task.ProgressData;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.lang.RunnablePR;




@Deprecated
public class SendCheckPassCMDTask extends TaskAdapter<Void,Void,InnerResponEntity<Void>> {

    private IHardwareControler mIHardwareControler;
    private Task<?, ReadResponEntity, ?> mReadTask;
    public SendCheckPassCMDTask(ITaskExecutor<Void, Void, InnerResponEntity<Void>> taskExecutor, TaskManager mTaskManager, Task<?, ReadResponEntity, ?> mReadTask,IHardwareControler mIHardwareControler) {

        super(taskExecutor, SendCheckPassCMDTask.class.getName(), mTaskManager);
        this.mIHardwareControler=mIHardwareControler;
        this.mReadTask=mReadTask;
    }



    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);

        
        Task<?, ReadResponEntity, ?> readTask=mReadTask;
        readTask.addProgresInterceptor(new ProgresInterceptor<ReadResponEntity>() {
            @Override
            public boolean onInterceptProgresing(Task<?, ReadResponEntity, ?> task,ProgressData<ReadResponEntity> progressData) {


                return false;
            }

            @Override
            public boolean interceptProgressUpdate(Task<?, ReadResponEntity, ?> task,ProgressData<ReadResponEntity> data) {

                ReadResponEntity readResponEntity=data.getData();
                if(readResponEntity==null)return false;

                mReadTask.removeProgresInterceptor(this);
                
                complete(readResponEntity.isSuccess()?new InnerResponEntity<Void>().success(null,""):new InnerResponEntity<Void>().fail(null,""));


                return false;
            }
        });

        mTaskExecutor.executorDoAsyncExecuting(new RunnablePR<Void, Void>() {
            @Override
            public Void run(Void bluetoothSocket) {
                doAsyncExecuting(bluetoothSocket);
                return null;
            }
        },param);
    }
    public void doAsyncExecuting(Void param) {


        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        boolean isSendSuccess=false;
        
        CMD_READ_CARD_NUM[CMD_READ_CARD_NUM.length-1-1]=HSJ800Help.crc((byte)0,(byte)0,CMD_READ_CARD_NUM);
        
        for (int i=0;i<3;i++) {
           if( verifyWrite(mIHardwareControler.write( CMD_READ_CARD_NUM))) {
               isSendSuccess=true;
               break;
           }
        }
        if(!isSendSuccess)
            complete(new InnerResponEntity<Void>().fail(null,""));
    }


    

    public static byte[] CMD_READ_CARD_NUM =

new byte[]{
        32
,  0
,   0x4C
,  8
,  0x00
,  1
,
        (byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff, -70
, 3

            };

    private boolean verifyWrite(int ret) {
        if (ret <= 0) {


            return false;
        }
        return true;
    }
}
